package com.example.Sao.Adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.Sao.MyBean.ExploreStause;
import com.example.Sao.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * @ProjectName: scan
 * @Package: com.example.MyApplication.Adapter
 * @ClassName: HistoryAdapter
 * @Description: 搜素历史数据记录适配器
 * @Author: 啊毛
 * @CreateDate: 2020/4/21 18:50
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/4/21 18:50
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.MViewHolder> {
    private HistoryCallBack historyCallBack;
    private boolean isExplore;
    private Context mContext;
    private ArrayList<String>history_list;

    public HistoryAdapter(Context mContext,ArrayList<String>history_list,boolean isExplore,HistoryCallBack historyCallBack){
        this.historyCallBack  = historyCallBack;
        this.isExplore = isExplore;
        this.mContext = mContext;
        this.history_list = history_list;
    }

    @Override
    public int getItemCount() {
        if(!isExplore) {
            return 6;
        }else{
            return history_list.size()+1;
        }
    }

    @NonNull
    @Override
    public MViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = View.inflate(mContext, R.layout.adpter_history,null);
        return new MViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MViewHolder mViewHolder, int i) {
        //小于5条历史数据并且不再展开状态
        if(!isExplore&&(i<=5&&i>1)) {
           final String word  = history_list.get(i);
            mViewHolder.textView.setText(word);
            mViewHolder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (historyCallBack!=null){
                        historyCallBack.historycallback(word);
                    }
                }
            });
            if(i==5){
                mViewHolder.textView.setText(".......");
                mViewHolder.textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ExploreStause stause = new ExploreStause();
                        stause.setEvent("explore_stause");
                        stause.setExplore(true);
                        EventBus.getDefault().post(stause);
                    }
                });
            }
        }else if (isExplore){
            if (i==history_list.size()){
                mViewHolder.textView.setText("");
                mViewHolder.textView.setBackgroundDrawable(mContext.getDrawable(R.drawable.ic_keyboard_arrow_up_black_24dp));
                mViewHolder.textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ExploreStause stause = new ExploreStause();
                        stause.setEvent("explore_stause");
                        stause.setExplore(false);
                        EventBus.getDefault().post(stause);
                    }
                });
            }else {
                final String word  = history_list.get(i);
                mViewHolder.textView.setText(word);
                mViewHolder.textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (historyCallBack!=null){
                            historyCallBack.historycallback(word);
                        }
                    }
                });
            }
        }
    }

     class MViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        public MViewHolder(View v){
            super(v);
            textView = v.findViewById(R.id.text4);
        }
    }

}
